package com.lijin.study.java.math;

import java.math.BigDecimal;
import java.util.Random;

import org.junit.Test;

public class MathTest {

	@Test
	public void bdTest() throws Exception {
		// BigDecimal bd = BigDecimal.valueOf(1234555);
		// System.out.println(bd.movePointLeft(2));
		// System.out.println(bd.movePointLeft(3));
		// System.out.println(bd.movePointLeft(4));
		// System.out.println(bd.movePointLeft(2));

		System.out.println(new BigDecimal("0.22").movePointRight(2));
		System.out.println(new BigDecimal("0.22").multiply(BigDecimal.valueOf(100)));

		int i = 1;

		System.out.println((++i + i++) + ++i);

		System.out.println(new BigDecimal("0.00"));

		BigDecimal b = new BigDecimal("0.00").subtract(new BigDecimal("0.22"));
		System.out.println(b);

		System.out.println(BigDecimal.valueOf(80).divide(BigDecimal.valueOf(100), 4,
				BigDecimal.ROUND_HALF_UP).movePointRight(2).doubleValue());
	}

	@Test
	public void intTest() throws Exception {
		System.out.println(Integer.toBinaryString(5 & 1));
		System.out.println(Integer.toBinaryString(5 & 8));
		System.out.println(Integer.toBinaryString(Integer.MIN_VALUE));
		System.out.println(Integer.toBinaryString(Integer.MIN_VALUE + 1));
		System.out.println(Integer.MIN_VALUE + 1);

		System.out.println(Integer.MAX_VALUE);
	}

	@Test
	public void testComp() throws Exception {
		int i = 10;
		System.out.println(i % 100);
		System.out.println(i / 100);
	}

	@Test
	public void testPlus() {
//		int i = 5;
//		int j = 0;
//		if (((j = ++i) == 6) && i == 6) {
//			System.out.println(i + ", " + j);
//		}
		
		BigDecimal d1 = new BigDecimal(0.00);
		System.out.println(d1.add(new BigDecimal("1222").movePointLeft(6)));
	}

	@Test
	public void randomNum() throws InterruptedException {
		System.out.println("-------");
		System.out.println(this.computeAddtion(2589, 10000));
		System.out.println("-------");
		System.out.println(this.computeAddtion(2589, 2589));
		System.out.println("-------");
		System.out.println(this.computeAddtion(2589, 1520));
		System.out.println("-------");
		System.out.println(this.computeAddtion(2589, 2590));
		System.out.println("-------");
		System.out.println(this.computeAddtion(2589, 3344));
		System.out.println("-------");
		System.out.println(this.computeAddtion(2589, 1000331));
		System.out.println("-------");
		System.out.println(this.computeAddtion(2589, 33234234));
		System.out.println("-------");
	}
	
	private int computeAddtion(int diff, int fltSize) {
		if (fltSize <= diff) {
			return fltSize;
		} else {
			int randInt = 0;
			Random rand = new Random();
			int[] array = {0, 1, 2, 3, 4, 5, 2, 3, 4, 1};
			for (int i = 9; i > 1; i--) {
				int index = rand.nextInt(i);
				int tmp = array[index];
				array[index] = array[i - 1];
				array[i - 1] = tmp;
			}
			for (int i = 0; i < 4; i++) {
				randInt = randInt * 10 + array[i];
			}
			System.out.println("随机数--->" + randInt);
			int result = diff + randInt;
			return result <= fltSize ? result : fltSize;
		}
	}
}
